<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    {{template "header.tpl" .}}
</head>
<body class="pear-container">

<div class="layui-col-md3">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 10px;">
            {{template "system/config/config.tpl" .}}
        </div>
    </div>
</div>
<div class="layui-col-md9">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 10px;">
            {{template "system/config/config_data.tpl" .}}
        </div>
    </div>
</div>
</body>
<!-- 编辑表单 -->
<script type="text/html" id="configEditDialog">
    <form id="configEditForm" lay-filter="configEditForm" class="layui-form model-form">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <input name="id" id="id" type="hidden" value="{{.info.Id}}">
                    <div class="layui-form-item">
                        <label class="layui-form-label layui-form-required">配置名称:</label>
                        <div class="layui-input-block">
                            <input name="name" value="{{.info.Name}}" placeholder="请输入配置名称" class="layui-input"
                                   lay-verType="tips" lay-verify="required"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label layui-form-required">排序号:</label>
                        <div class="layui-input-block">
                            <input name="sort" value="{{.info.Sort}}" placeholder="请输入排序号" class="layui-input"
                                   type="number"
                                   lay-verType="tips" lay-verify="required" required/>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="bottom">
            <div class="button-container">
                <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="configEditSubmit"
                        lay-submit><i class="layui-icon layui-icon-ok"></i>保存
                </button>
                <button class="pear-btn pear-btn-sm" type="reset"><i class="layui-icon layui-icon-refresh"></i>重置</button>
            </div>
        </div>

        {{/*<div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>*/}}
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="configDataTbBar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"></i></button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="del"><i class="pear-icon pear-icon-ashbin"></i></button>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="configDataEditDialog">
    <form id="configDataEditForm" lay-filter="configDataEditForm" class="layui-form model-form"
          style="padding-left: 10px;">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <input name="id" id="id" type="hidden" value="{{.info.Id}}">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">配置标题：</label>
                            <div class="layui-input-inline">
                                <input name="title" value="{{.info.Title}}" placeholder="请输入配置标题"
                                       class="layui-input"
                                       lay-verType="tips" lay-verify="required" required/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">配置编码：</label>
                            <div class="layui-input-inline">
                                <input name="code" value="{{.info.Code}}" placeholder="请输入配置编码"
                                       class="layui-input"
                                       lay-verType="tips" lay-verify="required" required/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">配置类型：</label>
                            <div class="layui-input-inline">
                                <select name="type" class="form-control">
                                    {{range $key,$v := .configTypeMap}}
                                        {{/* <option value="{{$elem}}" {{if compare $elem.cat_id $cid}} selected="selected" {{end}}>{{$elem.catname}}</option>*/}}
                                        <option value="{{$key}}">{{$v}}</option>
                                    {{end}}
                                </select>
                                {{/*{{select "Type|1|配置类型|name|id" .configTypeMap .info.Type}}*/}}
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">配置值：</label>
                            <div class="layui-input-inline">
                                <input name="value" value="{{.info.Value}}" placeholder="请输入配置值"
                                       class="layui-input"
                                       lay-verType="tips" lay-verify="required" required/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">排序号：</label>
                            <div class="layui-input-inline">
                                <input name="sort" value="{{.info.Sort}}" placeholder="请输入排序号" class="layui-input"
                                       type="number"
                                       lay-verType="tips" lay-verify="required" required/>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">配置项：</label>
                        <div class="layui-input-block">
                            <textarea name="options" placeholder="请输入配置项"
                                      class="layui-textarea">{{.info.Options}}</textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注:</label>
                        <div class="layui-input-block">
                            <textarea name="note" placeholder="请输入备注"
                                      class="layui-textarea">{{.info.Note}}</textarea>
                        </div>
                    </div>
                    {{/*<div class="layui-form-item text-right">
                        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="configDataEditSubmit"
                                lay-submit>保存
                        </button>
                        <button class="pear-btn pear-btn-sm" type="button" ew-event="closeDialog">取消</button>
                    </div>*/}}
                </div>
            </div>
        </div>

        <div class="bottom">
            <div class="button-container">
                <button class="pear-btn pear-btn-primary pear-btn-sm" lay-filter="configDataEditSubmit"
                        lay-submit><i class="layui-icon layui-icon-ok"></i>保存
                </button>
                <button class="pear-btn pear-btn-sm" type="reset"><i class="layui-icon layui-icon-refresh"></i>重置</button>
            </div>
        </div>
    </form>
</script>
{{template "footer.tpl" .}}
<script>
    /**
     * 配置管理
     * @author hanxuan
     * @since 2023/2/2
     */
    layui.use(['layer', 'form', 'table'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var selObj;  // 左表选中数据

        form.on('submit(configDataEditSubmit)', function (data) {
            console.log("1111")
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#configTable',
            url: '/system/config',
            height: 'full-100',
            method: 'get',
            toolbar: ['<p>',
                '<button lay-event="add" class="pear-btn pear-btn-md pear-btn-primary"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="edit" class="pear-btn pear-btn-md pear-btn-warming"><i class="layui-icon">&#xe642;</i>修改</button>&nbsp;',
                '<button lay-event="del" class="pear-btn pear-btn-danger pear-btn-md"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            defaultToolbar: [],
            cols: [[
                {field: 'id', width: 80, title: 'ID', align: 'center'}
                , {field: 'name', title: '配置名称'}
            ]],
            done: function (res, curr, count) {
                $('#configTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
            }
        });

        /* 表格搜索 */
        form.on('submit(configTbSearch)', function (data) {
            insTb.reload({where: data.field});
            return false;
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(configTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'edit') { // 修改
                showEditModel(selObj.data, selObj);
            } else if (obj.event === 'del') { // 删除
                doDel(selObj);
            }
        });

        /* 监听行单击事件 */
        table.on('row(configTable)', function (obj) {
            selObj = obj;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            insTb2.reload({where: {configId: obj.data.id}, page: {curr: 1}, url: '/system/configDetail'});
        });

        /* 显示表单弹窗 */
        function showEditModel(mData, obj) {
            layer.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '配置',
                area: ['360px', '230px'],
                content: $('#configEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('configEditForm', mData);
                    // 表单提交事件
                    form.on('submit(configEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? '/system/config/' + data.field.id : '/system/config/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (200 === res.status) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                if (obj) {
                                    obj.update(data.field);
                                } else {
                                    insTb.reload();
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除此配置吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);

                $.ajax({
                    url: "/system/config/" + obj.data.id,
                    //dataType: 'json',
                    type: 'delete',
                    success: function (res) {
                        layer.close(loadIndex);
                        if (200 === res.status) {
                            layer.msg(res.msg, {icon: 1});
                            obj.del();
                            $('#configTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        /* 渲染表格2 */
        var insTb2 = table.render({
            elem: '#configDataTable',
            data: [],
            //height: 'full-100',
            skin: 'line',
            height: 'full-148',
            method: 'get',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="pear-btn pear-btn-md pear-btn-primary"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="pear-btn pear-btn-danger pear-btn-md"><i class="layui-icon">&#xe640;</i>删除</button>&nbsp;',
                '</p>'].join(''),
            //cellMinWidth: 100,
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id',  title: 'ID', align: 'center', sort: true, fixed: 'left'}
                , {field: 'title',  title: '配置标题', align: 'center'}
                , {field: 'code',  title: '配置标签符', align: 'center'}
                , {field: 'value', title: '配置值', align: 'center'}
                , {field: 'options', title: '配置项', align: 'center'}
                , {field: 'type', title: '配置类型', align: 'center', templet(d) {
                        var cls = "";
                        return '<span class="pear-btn ' + cls + 'pear-btn-md pear-btn-primary">' + d.TypeName + '</span>';
                    }
                }
                /*, {field: 'Status', width: 100, title: '状态', align: 'center', templet: function (d) {
                        return  '<input type="checkbox" name="status" value="' + d.Id + '" lay-skin="switch" lay-text="正常|禁用" lay-filter="status" '+(d.Status==1 ? 'checked' : '')+'>';
                    }}*/
                , {field: 'sort', title: '排序号', align: 'center'}
                , {field: 'note', title: '配置说明', align: 'center'}
                , {field: 'createTime', title: '添加时间', align: 'center', templet:parent.cube.convertDateTime}
                , {field: 'updateTime', title: '更新时间', align: 'center', templet:parent.cube.convertDateTime}
                , {title: '操作', toolbar: '#configDataTbBar', align: 'center', fixed: 'right'}
            ]]
        });

        /* 表格2搜索 */
        form.on('submit(configDataTbSearch)', function (data) {
            insTb2.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格2工具条点击事件 */
        table.on('tool(configDataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel2(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel2(obj);
            }
        });

        /* 表格2头工具栏点击事件 */
        table.on('toolbar(configDataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel2();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('configDataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel2({ids: ids});
            }
        });

        /* 显示表单弹窗2 */
        function showEditModel2(mData) {
            layer.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '配置项',
                content: $('#configDataEditDialog').html(),
                area: ['750px', '530px'],
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('configDataEditForm', mData);
                    // 表单提交事件
                    form.on('submit(configDataEditSubmit)', function (data) {
                        data.field.configId = selObj.data.id;
                        var loadIndex = layer.load(2);
                        $.post(mData ? '/system/configDetail/' + selObj.data.id : '/system/configDetail/add', data.field, function (res) {
                            layer.close(loadIndex);
                            if (200 === res.status) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb2.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 删除2 */
        function doDel2(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                var ids = []
                if (obj.data) {
                    ids = [obj.data.id]
                } else if (obj.ids) {
                    ids = obj.ids;
                }
                $.post('/system/configDetail/delete', {id: ids.join(",")}, function (res) {
                    layer.close(loadIndex);
                    if (200 === res.status) {
                        layer.msg(res.msg, {icon: 1});
                        insTb2.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        //【设置状态】
        // func.formSwitch('status', "/configdata/setStatus", function (data, res) {
        //     console.log("开关回调成功");
        // });

    });

</script>
</html>